<?php
use yii\bootstrap\ActiveForm;
use common\helps\tools;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<style>
    .dataTable>tbody>tr>td{
        vertical-align: middle;
        font-size: 0.8em;
        padding: 8px 18px;
    }
    .dataTable>thead>tr>th{
        vertical-align: middle;
        font-size: 0.8em;
        text-align: center;
    }
    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    label{
        text-align: right;
    }
    #table_id_example th, td {
        white-space: nowrap;
    }
    #table_list th,td{ font-family: '微软雅黑'; font-size: small; text-align: center}

</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">

                    <div class="col-md-12" style="margin-top: 10px">
                        <?php if($bill) { ?>
                            <div style="height: 30px; font-size: 14px">单号：<?= $bill['billNo']?></div>
                        <?php }?>
                        <div style="float: left">
                            仓库：<select id="warehouseId" name="warehouseId" class="depart-input" style="width: 200px">
                                <option value="">选择仓库</option>
                                <?php foreach($warehouse as $value) { ?>
                                    <option value="<?= $value['id']?>" <?=($bill&&$bill['warehouseId']==$value['id'])?'selected':'' ?>><?= $value['warehouseName']?></option>
                                <?php }?>
                            </select>&nbsp;&nbsp;
                            <button type="button" class="btn btn-default" id="wh-confirm-btn" style="width:70px;">确认</button>&nbsp;&nbsp;
                            <button type="button" class="btn btn-primary" id="show-materiel-btn" style="width: 100px;">查询</button>&nbsp;&nbsp;
                            <?php if(!$bill || $bill['billStatus'] == 0) { ?>
                                <button type="button" class="btn btn-primary" id="match-stock-btn" style="width: 100px;">匹配库存</button>
                            <?php } ?>
                        </div>
                        <div style="float: right">
                            门店：<select id="shopId" name="shopId" class="depart-input" style="width: 200px">
                                <option value="">选择门店</option>
                                <?php foreach($shop as $value) { ?>
                                    <option value="<?= $value['ShopID']?>" info="<?= $value['Address']?>" <?=($bill&&$bill['shopId']==$value['ShopID'])?'selected':'' ?>><?= $value['ShopName']?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>

                    <div class="col-md-12" style="margin-top: 30px">
                        <table id="table_list" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th style="width: 10%">物料ID</th>
                                <th style="width: 13%">物料编码</th>
                                <th style="width: 13%">物料名称</th>
                                <th style="width: 12%">所属仓库</th>
                                <th style="width: 10%">订货数量</th>
                                <th style="width: 10%">发货数量</th>
                                <?php if($bill && $bill['billStatus'] == 3) { ?>
                                    <th style="width: 10%">收货数量</th>
                                    <th style="width: 10%">图片</th>
                                    <th style="width: 15%">备注</th>
                                <?php } ?>
                                <th style="width: 10%">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if($bill && $bill['dtl']){ foreach ($bill['dtl'] as $dtl) { ?>
                                <tr data-code="<?= $dtl['materielId'] ?>">
                                    <td><?= $dtl['materielId'] ?></td>
                                    <td><?= $dtl['materielCode'] ?></td>
                                    <td><?= $dtl['materielName'] ?></td>
                                    <td><?= $dtl['warehouseName'] ?></td>
                                    <td><input class="depart-input" type="text" name="orderQty" data-Flg="<?= $dtl['storeFlg'] ?>" style="width:80px; height: 22px" value="<?= $dtl['qty'] ?>"></td>
                                    <td><span name="deliverQty" <?= $dtl['deliverQty']!=$dtl['qty'] ? 'style="color:red"':'' ?>><?= $dtl['deliverQty'] ?></span></td>
                                    <?php if($bill && $bill['billStatus'] == 3) { ?>
                                        <td><?= $dtl['receiptQty'] ?></td>
                                        <td> <?php if($dtl['extFlg']) {?><a name="show-img">【查看】</a><?php } ?></td>
                                        <td><?= $dtl['remark'] ?></td>
                                    <?php } ?>
                                    <td>
                                    <?php if(!in_array($bill['billStatus'],array('2','3','9'))) { ?>
                                    <?php if($dtl['storeFlg']) { ?>
                                        <a name="match-stock">【匹配库存】</a>&nbsp;&nbsp;
                                    <?php } ?>
                                        <a name="del-materiel">【删除】</a>
                                    <?php } ?>
                                    </td>
                                </tr>
                            <?php }} ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="col-md-12">
                        <div style="float: left; line-height: 50px">
                            <span>发货时间：</span>
                            <input class="depart-input" type="text" name="sendDate" id="sendDate" style="width: 200px" value="<?=$bill ? $bill['sendDate']: $sendDate ?>">
                        </div>
                        <div style="float: right; line-height: 50px">
                            <span>收货信息：</span><span id="address"><?=$bill ? $bill['address']:'' ?></span>
                        </div>
                    </div>
                    <div class="col-md-12" style="margin-top: 10px">
                        <div style="float: left;">
                            <span style="float: left">订单备注：</span>
                            <textarea style="float: left" name="remark" id="remark" cols="65" rows="4"><?=$bill ? $bill['orderDesc']:'' ?></textarea>
                        </div>
                        <div style="float: right;">
                            <?php if($bill) { ?>
                            创建人：<?= $bill['creator']?>&nbsp;&nbsp;创建时间：<?= $bill['createTime']?><br>
                            <?= $bill['editor']?'修改人：'.$bill['editor']:'' ?>&nbsp;&nbsp;<?= $bill['editTime']?'修改时间：'.$bill['editTime']:'' ?>
                            <?php } else {?>&nbsp;<?php }?>
                        </div>
                        <div style="clear: both">&nbsp;</div>
                    </div>
                </div>
                <div class="box-footer" style="text-align: right;">
                    <input id="billNo" type="hidden" value="<?=$bill ? $bill['billNo']:'' ?>">
                    <button type="button" class="btn btn-default" onclick="history.back();">返回</button>
                    <button type="button" class="btn btn-primary hideBtn" onclick="saveRevise()">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 查询物料 -->
<div class="modal fade" id="add-materiel-modal" style="background: rgba(236, 230, 230, 0.3);" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加物料</h4>
            </div>
            <div class="modal-body">
                <div class="box-header">
                    物料名称/编号: <input class="depart-input" id="searchKey" type="text">
                    <button type="button" class="btn btn-primary depart-modal" id="search-btn" style="width: 100px;">查询</button> &nbsp;
                </div>
                <div class="box-body">
                    <table id="search_list" class="display">
                        <thead>
                        <tr>
                            <th style="width: 13%">物料ID</th>
                            <th style="width: 13%">物料编码</th>
                            <th style="width: 13%">物料名称</th>
                            <th style="width: 13%">所属仓库</th>
                            <th style="width: 13%">规格描述</th>
                            <th style="width: 13%">包装方式</th>
                            <th style="width: 13%">采购单位</th>
                            <th style="width: 12%">订货单位</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="add-materiel-btn">添加</button>
            </div>
        </div>
    </div>
</div>

<!-- 查询物料库存 -->
<div class="modal fade" id="add-stock-modal" style="background: rgba(236, 230, 230, 0.3);" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">匹配库存</h4>
            </div>
            <div class="modal-body">
                <div class="box-body">
                    <table id="search_stock_list" class="display">
                        <thead>
                        <tr>
                            <th style="width: 9%">物料ID</th>
                            <th style="width: 9%">物料编码</th>
                            <th style="width: 9%">物料名称</th>
                            <th style="width: 10%">所属仓库</th>
                            <th style="width: 9%">供应商</th>
                            <th style="width: 9%">批次</th>
                            <th style="width: 9%">单价</th>
                            <th style="width: 9%">总数</th>
                            <th style="width: 9%">冻结数</th>
                            <th style="width: 9%">可用数</th>
                            <th style="width: 10%">发货数量</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="add-stock-btn">添加</button>
            </div>
        </div>
    </div>
</div>

<!-- 查看图片 -->
<div class="modal fade" id="show-img-modal" style="background: rgba(236, 230, 230, 0.3);" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 50%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">查看图片</h4>
            </div>
            <div class="modal-body">
                <div class="box-body" id="dtlImgBox" style="text-align: center">

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="/static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/static/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script type="text/javascript" src="/static/app/bill/detail.js?version=<?=Yii::$app->params['version']?>"></script>
<script type="text/javascript">
    var tempData = {},          //物料临时数据
        selectData = {},        //物料提交数据
        mDataTable = null,      //物料全局dataTable
        sDataTable = null,      //库存全局dataTable
        currCode = null,        //匹配库存当前物料
        confirmFlg = 0,         //确认仓库标记
        dateFlg = 0;            //物料列表刷新标识

    <?php if($bill) { ?>
    <?php if($bill['dtl']) { foreach ($bill['dtl'] as $dtl) { ?>
    selectData['<?= $dtl['materielId'] ?>'] = {
        materielId: '<?= $dtl['materielId'] ?>',
        materielCode: '<?= $dtl['materielCode'] ?>',
        materielName: '<?= $dtl['materielName'] ?>',
        warehouseName: '<?= $dtl['warehouseName'] ?>',
        orderQty: '<?= $dtl['qty'] ?>',
        storeFlg: '<?= $dtl['storeFlg'] ?>',
        batch: {}
    };
    <?php }} ?>

    <?php if($bill['batch']) { foreach ($bill['batch'] as $batch) { ?>
    selectData['<?= $batch['materielId'] ?>']['batch'][<?= $batch['stockId'] ?>] = {
        stockId: '<?= $batch['stockId'] ?>',
        batch: '<?= $batch['batch'] ?>',
        price: '<?= $batch['price'] ?>',
        deliverQty: '<?= $batch['deliverQty'] ?>',
    };
    <?php }} ?>

    <?php if(in_array($bill['billStatus'],array('2','3','9')) || ($bill['billStatus'] == 1 && !empty($bill['ext1']))) { ?>
    $('input,textarea,select').attr("disabled","disabled");
    $('#wh-confirm-btn').hide();
    $('#show-materiel-btn').hide();
    $('.hideBtn').hide();
    <?php } else { ?>
    confirmFlg = 1;
    $('#wh-confirm-btn').text('重置');
    $('#warehouseId').attr('disabled','true').css('color', '#ccc');
    <?php }} ?>

    //自动匹配库存
    $('#match-stock-btn').on('click',function () {

        if ($("#warehouseId").val() == '') {
            $.jBox.tip('请选择仓库');
            return;
        }
        if ($.isEmptyObject(selectData)) {
            $.jBox.tip('请添加物料信息');
            return;
        }

        var errMsg = '', materielData = [];
        $.each(selectData, function (i, v) {
            if(v['storeFlg']=='0') return true;
            if (!v.orderQty) {
                errMsg = '请填写[' + v.materielName + ']的订货数量';
                return false;
            }
            if (!v.hasOwnProperty('batch') || $.isEmptyObject(v['batch'])) {
                materielData.push({materielId: v['materielId'], qty: v['orderQty']})
            }
        });

        if(errMsg) {
            $.jBox.error(errMsg, '错误');
            return;
        }

        if(!materielData.length) return;
        $.post('/bill/match-stock', {'warehouseId': $('#warehouseId').val(), 'materielData': materielData},
            function (o) {
                if (o.code == 200) {
                    if(o.data) {
                        $.each(o.data, function (i, v) {
                            if(v['deliverQty']){
                                if(!selectData[i]['batch']) selectData[i]['batch'] = {};
                                $.each(v['batch'], function (i2, v2) {
                                    selectData[i]['batch'][v2.id] = {
                                        stockId: v2.id,
                                        batch: v2.batch,
                                        price: v2.price,
                                        deliverQty: v2.useCnt
                                    };
                                });
                                $("#table_list").find('tr[data-code="' + i + '"]').find('span[name=deliverQty]').html(v['deliverQty']).css('color', 'green');
                            }else {
                                $("#table_list").find('tr[data-code="' + i + '"]').find('span[name=deliverQty]').css('color', 'red');
                            }
                        });
                    }
                } else {
                    $.jBox.error(o.val, '错误');
                }
            },
            'json'
        );

    });
</script>
